package com.yxw.live_vod_boot.service.impl;



import lombok.extern.slf4j.Slf4j;
//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yxw.yxnet_cd_center.common.utils.BaseResult;
import com.yxw.yxnet_cd_center.common.utils.BaseDateTime;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yxw.live_vod_boot.entity.YxLiveShopReportEntity;
import com.yxw.live_vod_facade.dto.YxLiveShopReportDTO;
import com.yxw.live_vod_boot.dto.LiveShopReporAddtDTO;
import com.yxw.live_vod_facade.dto.LiveShopReporManagerQueryDTO;
import com.yxw.live_vod_boot.dto.LiveShopReporUserQueryDTO;
import com.yxw.live_vod_boot.mapper.YxLiveShopReportMapper;
import com.yxw.live_vod_boot.service.IYxLiveShopReportService;
import com.yxw.live_vod_boot.utils.HuaWeiObsUtil;
import com.yxw.yxnet_cd_center.common.constant.CommonConstant;
import com.yxw.yxnet_cd_center.common.dto.BaseUserDTO;
import com.yxw.yxnet_cd_center.common.enums.FileClassifyEnum;
import com.yxw.yxnet_cd_center.common.exception.BaseException;
import com.yxw.yxnet_cd_center.common.utils.TokenUtil;
import java.io.IOException;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.multipart.MultipartFile;
import com.obs.services.ObsClient;
import com.obs.services.model.PutObjectResult;

/*** 
*@author Jiaxu.Huang
*@date 2023/12/8 17:19:52
*@update 2023/12/8 17:19:52
**/
@Slf4j
@Service
//public class YxLiveShopReportServiceImpl  extends ServiceImpl<YxLiveShopReportMapper, YxLiveShopReportEntity> implements IYxLiveShopReportService{
public class YxLiveShopReportServiceImpl  implements IYxLiveShopReportService{
    /**
     * 图片文件最大大小
     */
    @Value("${file.image.max.size}")
    private long fileImageMaxSize;
    @Autowired
    private HuaWeiObsUtil huaWeiObsUtil;
    
    @Autowired
    private YxLiveShopReportMapper oYxLiveShopReportMapper;
    @Autowired
    private SysNoticeServiceImpl oSysNoticeServiceImpl;     

    
    /*** 
    * 保存
    * @param oYxLiveShopReportDTO
    * @return BaseResult
    **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResult insertYxLiveShopReport(HttpServletRequest httpRequest,MultipartFile accusationPicture,LiveShopReporAddtDTO oLiveShopReporAddtDTO)
    {       
        YxLiveShopReportEntity oYxLiveShopReportEntity = oLiveShopReporAddtDTO.ConverToEntity();//new YxLiveShopReportEntity();
        //封面大小校验
        if(!accusationPicture.isEmpty())
        {
            if (accusationPicture.getSize() > fileImageMaxSize) {
                return BaseResult.failed("举报的图片不能上传超过" + fileImageMaxSize / 1048576.0 + "M<br>");
            }
        }
        String accusationPictureKey = "";
        if(!accusationPicture.isEmpty())
        {
            String obsDownloadUrlStartPoint = huaWeiObsUtil.getObsDownloadUrlStartPointNoEndSlash();
            //accusationPictureKey = FileClassifyEnum.LIVE_FILE.getPath() + BaseDateTime.getShortDateStringNoSlipt() + "/" + UUID.randomUUID().toString().replace("-", "") + accusationPicture.getOriginalFilename().substring(accusationPicture.getOriginalFilename().lastIndexOf(CommonConstant.POINT));
            accusationPictureKey = UUID.randomUUID().toString().replace("-", "") + accusationPicture.getOriginalFilename().substring(accusationPicture.getOriginalFilename().lastIndexOf(CommonConstant.POINT));
            if(accusationPictureKey.startsWith("/"))
            {
                oYxLiveShopReportEntity.setAccusationPictureUrl(obsDownloadUrlStartPoint.concat(accusationPictureKey));
            }
            else
            {
                oYxLiveShopReportEntity.setAccusationPictureUrl(obsDownloadUrlStartPoint.concat("/").concat(accusationPictureKey));
            }
        }
        oYxLiveShopReportEntity.setCreateTime(BaseDateTime.getDate());
        oYxLiveShopReportMapper.insert(oYxLiveShopReportEntity);        
        if(!accusationPicture.isEmpty())
        {
            //举报的图片上传华为云            
            try
            {  
                PutObjectResult oPutObjectResult = huaWeiObsUtil.uploadFileInputStream(accusationPictureKey, accusationPicture.getInputStream());
            }
            catch (IOException e)
            {  
                log.error("举报的图片上传异常："+e.getMessage());
                throw new BaseException("举报的图片上传："+e.getMessage());
            } 
        }
        return BaseResult.success(oYxLiveShopReportEntity);
    }
    /*** 
    * 修改
    * @param oYxLiveShopReportDTO
    * @return BaseResult
    **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResult managerAccessLiveShopReport(HttpServletRequest httpRequest,YxLiveShopReportDTO oYxLiveShopReportDTO)
    {
        UpdateWrapper<YxLiveShopReportEntity> ShopReportUpdateWrapper = new UpdateWrapper();
        ShopReportUpdateWrapper.eq("report_id", oYxLiveShopReportDTO.getReporId());
        ShopReportUpdateWrapper.set("access_result", oYxLiveShopReportDTO.getAccessResult());
        ShopReportUpdateWrapper.set("access_user_id", oYxLiveShopReportDTO.getAccessUserId());
        ShopReportUpdateWrapper.set("access_user_name", oYxLiveShopReportDTO.getAccessUserName());
        oYxLiveShopReportMapper.update(null,ShopReportUpdateWrapper);
        return BaseResult.success();
    }
    /*** 
    * 删除
    * @param id
    * @return BaseResult
    **/
    //@Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResult deleteYxLiveShopReportById(HttpServletRequest httpRequest,Long id)
    {
        if(ObjectUtil.isEmpty(id))
        {
            return BaseResult.failed("Id不能为空值");
        }
        oYxLiveShopReportMapper.deleteById(id);
        return BaseResult.success();
    }
    /*** 
    * 根据id查找
    * @param id
    * @return YxLiveShopReportEntity
    **/
    @Override
    public YxLiveShopReportEntity selectYxLiveShopReportById(HttpServletRequest httpRequest,Long id)
    {
        if(ObjectUtil.isEmpty(id))
        {
            return null;
        }
        return oYxLiveShopReportMapper.selectById(id);
    }
    /*** 
    * 自定义查询
    * @param oYxLiveShopReportDTO
    * @return List<YxLiveShopReportEntity>
    **/
    @Override
    public BaseResult userQuewryLiveShopReportPage(HttpServletRequest httpRequest,int currentPage,int pageSize,LiveShopReporUserQueryDTO oLiveShopReporUserQueryDTO)
    {
        BaseUserDTO oBaseUserDTO = getBaseUserInfo(httpRequest,oLiveShopReporUserQueryDTO.getUserId());
        if(null == oBaseUserDTO)
        {
            return BaseResult.failed("请先登录");
        }
        Page<YxLiveShopReportEntity> pageParams = new Page<>(currentPage,pageSize);
        QueryWrapper<YxLiveShopReportEntity> oYxLiveShopReportQueryWrapper = new QueryWrapper();

        oYxLiveShopReportQueryWrapper.eq("accusation_user_id",oBaseUserDTO.getUserId());
        oYxLiveShopReportQueryWrapper.eq(ObjectUtil.isNotEmpty(oLiveShopReporUserQueryDTO.getAccessResult()),"access_result",oLiveShopReporUserQueryDTO.getAccessResult());
        return  BaseResult.success(oYxLiveShopReportMapper.selectPage(pageParams,oYxLiveShopReportQueryWrapper));
    }

    /*** 
    * 自定义分页查询
    * @param currentPage 当前页
    * @param pageSize 分页大小
    * @param oYxLiveShopReportEntity
    * @return Page
    **/
    @Override
    public BaseResult managerQuewryLiveShopReportPage(HttpServletRequest httpRequest,int currentPage,int pageSize,LiveShopReporManagerQueryDTO oLiveShopReporManagerQueryDTO)
    {        
        Page<YxLiveShopReportEntity> pageParams = new Page<>(currentPage,pageSize);
        QueryWrapper<YxLiveShopReportEntity> oYxLiveShopReportQueryWrapper = new QueryWrapper();
        oYxLiveShopReportQueryWrapper.like(ObjectUtil.isNotEmpty(oLiveShopReporManagerQueryDTO.getShopName()),"shop_name",oLiveShopReporManagerQueryDTO.getShopName());
        oYxLiveShopReportQueryWrapper.like(ObjectUtil.isNotEmpty(oLiveShopReporManagerQueryDTO.getLiveHomeName()),"live_home_name",oLiveShopReporManagerQueryDTO.getLiveHomeName());
        oYxLiveShopReportQueryWrapper.eq(ObjectUtil.isNotEmpty(oLiveShopReporManagerQueryDTO.getAccessResult()),"access_result",oLiveShopReporManagerQueryDTO.getAccessResult());
        return BaseResult.success(oYxLiveShopReportMapper.selectPage(pageParams, oYxLiveShopReportQueryWrapper));
    }
    private BaseUserDTO getBaseUserInfo(HttpServletRequest httpRequest,String userId)
    {
        String token = httpRequest.getHeader("token");
        if(StrUtil.isBlank(token))
        {
            token = "";
        }
        BaseUserDTO oBaseUserDTO = TokenUtil.getUserNotAuth(token);
        if(StrUtil.isNotBlank(userId))
        {
            oBaseUserDTO.setUserId(userId);
        }
        return oBaseUserDTO;
    }
}
    

